// FileDownloader.h
#ifndef FILEDOWNLOADER_H
#define FILEDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>

class FileDownloader : public QObject
{
    Q_OBJECT
public:
    explicit FileDownloader(QObject* parent = nullptr);

    // 可调用方法 - 开始下载
    void download(const QUrl& url, const QString& savePath);

    // 可调用方法 - 取消下载
    void cancel();

signals:
    // 进度信号
    void progress(qint64 bytesReceived, qint64 bytesTotal);

    // 完成信号
    void finished(bool success, const QString& filePath);

    // 错误信号
    void error(const QString& errorString);

private slots:
    void onReadyRead();
    void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
    void onFinished();

private:
    QNetworkAccessManager* m_manager;
    QNetworkReply* m_reply;
    QFile m_file;
    bool m_isDownloading;
};

#endif // FILEDOWNLOADER_H